<#include "/templates/function.ftl">
<#assign pageName="新增角色信息">
<#assign depth = 0 />  
<#assign separator="<span style='padding:0 10px;'>&nbsp;</span>">
<#macro buildMenuList _menuList _upperid>  
    <#if _menuList?? && _menuList?size gt 0>  
        <#list _menuList as dataItem>
            <#if dataItem.upperId == _upperid>
            <tr>
                <td class="align-center" width="50px">
                    <input type="checkbox" name="menuItem" value="${dataItem.menuId}" lay-filter="checkone" lay-skin="primary" 
                        <#if setMenuStatus(dataItem.menuId)>checked="checked"</#if>>
                </td>
                <td class="align-center" width="50px" nowrap="nowrap">
                    ${dataItem_index+1}
                </td>
                <td class="align-center" nowrap="nowrap">
                    ${dataItem.orderNo}
                </td>
                <td nowrap="nowrap">
                    <#list 0..depth as i>${separator}</#list>
                    <i class="${dataItem.icon} "></i>
                    ${dataItem.menuCaption}
                </td>
                <td class="align-center layui-hide-xs" >
                    <span>${dataItem.guid}</span>
                </td>
                <td class="layui-hide-xs">
                    <a href="${dataItem.httpAddress}" target="_blank">${dataItem.httpAddress}</a>
                </td>
            </tr>
            <#assign depth = depth + 1 />  
            <@buildMenuList _menuList,dataItem.menuId />  
            <#assign depth = depth - 1 />
            </#if>
        </#list>  
    </#if>  
</#macro> 

<#function setMenuStatus menuId>
    <#list menuSelected as menuItem>
        <#if menuItem?string==menuId?string>
            <#return true>
        </#if>
    </#list>
    <#return false>
</#function>

<#function setResourcesStatus resId>
    <#list resSelected as resItem>
        <#if resItem?string==resId?string>
            <#return true>
        </#if>
    </#list>
    <#return false>
</#function>
<@override name="head-styles">
</@override>
<@override name="page-content">
    <#if depList?has_content>
    <form class="layui-form padding-10" lay-filter="formRolesAdd">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
              <li class="layui-this">角色基本信息</li>
              <li>菜单权限设置</li>
              <li>资源权限设置</li>
            </ul>
            <div class="layui-tab-content" style="margin-top:40px;">
                <div class="layui-tab-item layui-show">
                    <@ctrl.TreeSelect path="roles.depId" dataList=depList![] fieldText="depName" 
                         fieldValue="depId" labelText="所属机构" fieldParentID="upperId" 
                         parentidValue="${(dep.upperId)!'-1'}" required="required">
                     </@ctrl.TreeSelect>
                     <@ctrl.SingleSelect path="roles.buildIn" options={'0':'否','1':'是'} labelText="内置角色" required="required"/>
                     <@ctrl.Textbox path="roles.roleName" labelText="角色名称" required="required"/>
                     <@ctrl.Textbox path="roles.roleCode" labelText="角色编码" required="required"/>
                     <@ctrl.Textarea path="roles.memo" labelText="角色描述" />
                     <@ctrl.Hidden path="roles.guid"/>
                </div>
                <div class="layui-tab-item">
                    <table class="layui-table">
                        <thead>
                            <tr>
                                <th class="align-center" width="50px">
                                      <input type="checkbox" lay-skin="primary" lay-filter="checkall">
                                </th>
                                <th class="align-center" width="50px" nowrap="nowrap">序号</th>
                                <th class="align-center" nowrap="nowrap">排序号</th>
                                <th class="align-center" nowrap="nowrap">菜单名称</th>
                                <th class="align-center layui-hide-xs">GUID</th>
                                <th class="align-center layui-hide-xs">链接地址</th>
                            </tr>
                        </thead>
                        <tbody>
                            <#if menuList?has_content>
                                <@buildMenuList menuList,"-1" />
                            <#else>
                                <td colspan="6" style="text-align:center;color:red;font-weight:bold;">暂无菜单信息</td>
                            </#if>
                        </tbody>
                    </table>
                </div>
                <div class="layui-tab-item">
                     <table class="layui-table">
                         <thead>
                             <tr>
                                 <th class="align-center" width="50px">
                                      <input type="checkbox" lay-skin="primary" lay-filter="checkall">
                                 </th>
                                 <th class="align-center" width="50px" nowrap="nowrap">序号</th>
                                 <th class="align-center" nowrap="nowrap">资源类型</th>
                                 <th class="align-center" nowrap="nowrap">资源名称</th>
                                 <th class="align-center" nowrap="nowrap">资源内容</th>
                                 <th class="align-center layui-hide-xs">优先级</th>
                                 <th class="align-center layui-hide-xs">说明</th>
                             </tr>
                         </thead>
                         <tbody>
                         <#if resList?has_content>
                             <#list resList as dataItem>  
                             <tr>
                                 <td class="align-center" width="50px">
                                     <input type="checkbox" name="resItem" value="${dataItem.resId}"  lay-skin="primary" lay-filter="checkone"
                                        <#if setResourcesStatus(dataItem.resId)>checked="checked"</#if>>
                                 </td>
                                 <td class="align-center" width="50px" nowrap="nowrap">
                                     ${dataItem_index+1}
                                 </td>
                                 <td class="align-center" nowrap="nowrap">
                                     ${dataItem.type}
                                 </td>
                                 <td nowrap="nowrap">
                                     ${dataItem.name}
                                 </td>
                                 <td nowrap="nowrap">
                                     ${dataItem.content}
                                 </td>
                                 <td class="layui-hide-xs" >
                                     ${dataItem.priority}
                                 </td>
                                 <td class="layui-hide-xs" >
                                    ${dataItem.memo}
                                 </td>
                             </tr>
                             </#list>
                         <#else>
                             <td colspan="7" style="text-align:center;color:red;font-weight:bold;">暂无资源信息</td>
                         </#if>
                         </tbody>
                     </table>
                </div>
             </div>
          </div>
          <@ctrl.FormAction backURL="../management?gid=${(roles.guid)!}"/>
      </form>
      <#else>
          <div class="">
             <div class="">未获取到所在平台当前用户机构信息，无法创建新的角色!</div>
          </div>
          <div class="">
              <@ctrl.FormAction62 buttons=['back'] backURL="../management?gid=${(roles.guid)!}"/>
          </div>
      </#if>
</@override>
<@override name="page-scripts">
    <script>
        $(function(){
            layui.use(['element', 'form','layer', 'jquery'], function(){
                var element = layui.element;
                var form = layui.form;
                var layer=layui.layer;
                $=layui.jquery;
                //监听表单提交
                form.on('submit(submit)', function(data){
                    var checkedMenu =[]; 
                    var checkedRes =[]; 
                    $('input[name="menuItem"]:checked').each(function(index, item){ 
                        checkedMenu.push($(this).val()); 
                    });
                    $('input[name="resItem"]:checked').each(function(index, item){ 
                        checkedRes.push($(this).val()); 
                    });
                    data.field.checkedMenu = checkedMenu;
                    data.field.checkedRes = checkedRes;
                    submitData(data.field, '${contextPath}/admin/roles/add', function(){location.href="${contextPath}/admin/roles/management?gid=${guid}";});
                    return false;
                });
                //复选框全选
                form.on('checkbox(checkall)', function (data) {
                   var child = $(data.elem).parents('.layui-tab-item').find('input[type="checkbox"]');
                   child.each(function (index, item) {
                       item.checked = data.elem.checked;
                   });
                   form.render('checkbox');
                });
                //复选框单选
                form.on('checkbox(checkone)', function (data) {
                    var child = $(data.elem).parents('.layui-tab-item').find('input[type="checkbox"]').not(":first");
                    var all = child.length;
                    child.each(function (index, item) {
                        if (item.checked == false) {
                            $(data.elem).parents('.layui-tab-item').find('input[type="checkbox"]').eq(0).prop("checked", false);
                            form.render('checkbox');
                            return false;
                        }else{
                            all--;
                        }
                    });
                    if(all==0){
                        $(data.elem).parents('.layui-tab-item').find('input[type="checkbox"]').eq(0).prop("checked", true);
                        form.render('checkbox');
                     }
                });
            });
        })
    </script>
</@override>
<@extends name="/layout/basepage.html"/> 